package com.swinginwind.flexutils.components.timeComp.time.timeChooser
{
	/**
	 * 描述: 时间选择组件
	 * @版权所有: horizo
	 * @创建者: caoww
	 * @创建日期: 2011-1-24 
	 * @修改者: 
	 * @修改日期: 
	 * @修改说明: 
	 */
	import com.swinginwind.flexutils.components.timeComp.common.helper.*;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Spacer;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.BorderContainer;
	import spark.components.ComboBox;
	import spark.components.NumericStepper;
	import spark.events.IndexChangeEvent;
	import spark.layouts.VerticalLayout;
	import com.swinginwind.flexutils.components.timeComp.time.timeField.TimeField;
	
	[Event(name="change", type="flash.events.Event")]
	public class TimeChooser extends BorderContainer
	{
		public function TimeChooser()
		{
			super();			
		}
		
		private var _selectedDate:Date;
		
		private var _timeField:TimeField;
		
		public function get timeField():TimeField
		{
			return _timeField;
		}

		public function set timeField(value:TimeField):void
		{
			_timeField = value;
		}

		public function get selectedDate():Date
		{
			return _selectedDate;
		}
		[Bindable("change")]
		public function set selectedDate(value:Date):void
		{
			_selectedDate = value;
			this.dispatchEvent(new Event("change"));
		}
		private var _selectedDateStr:String;
		
		public function get selectedDateStr():String
		{
			return _selectedDateStr;
		}
		
		[Bindable("textChanged")]
		public function set selectedDateStr(value:String):void
		{
			if(_selectedDateStr != value){
				_selectedDateStr = value;
				this.invalidateProperties();
				dispatchEvent(new Event("textChanged"));
			}
		}

		
		
		/**
		 * 设置控件中可选的第一个年份,默认值1900
		 */
		public var minYear:int=1900;
		/**
		 * 控件中可选的最后一个年份,默认值2100
		 */
		public var maxYear:int=2100;
		
		/**
		 * 文本字段中所显示日期的格式；默认值：YYYY/MM/DD JJ:NN:SS
		 */
		public var formatString:String="YYYY/MM/DD JJ:NN:SS";
		//私有组件
		private var yearCombobox:ComboBox;
		private var monthCombobox:ComboBox;
		private var dateGroup:DateGroup;
		private var hourNS:NumericStepper;
		private var minuteNS:NumericStepper;
		private var secondNS:NumericStepper;
		override protected function createChildren():void{
			super.createChildren();
			var lay:VerticalLayout=new VerticalLayout();
			lay.paddingLeft=0;
			lay.paddingRight=0;
			lay.paddingBottom=0;
			lay.paddingTop=0;
			lay.gap=0;
			lay.horizontalAlign="center";
			this.layout=lay;			
			
			var borderBox:BorderContainer;
			borderBox=MethodTimeChooser.createBorderBox();
			yearCombobox=new ComboBox();			
			yearCombobox.width=60;
			yearCombobox.dataProvider=MethodTimeChooser.createYearData(minYear,maxYear);
			yearCombobox.addEventListener(FlexEvent.CREATION_COMPLETE,function():void{				
				yearCombobox.textInput.restrict="0-9";
				yearCombobox.textInput.maxChars=4;
			});
			yearCombobox.addEventListener(IndexChangeEvent.CHANGE,function():void{
				//yearChange
				setNowTime(true);
			});
			borderBox.addElement(yearCombobox);
			borderBox.addElement(MethodTimeChooser.createLabel("年"));
			
			var sp:Spacer=new Spacer();
			sp.width=20;
			borderBox.addElement(sp);
			
			monthCombobox=new ComboBox();			
			monthCombobox.width=45;
			monthCombobox.dataProvider=MethodTimeChooser.createMonthData();
			yearCombobox.addEventListener(FlexEvent.CREATION_COMPLETE,function():void{				
				monthCombobox.textInput.restrict="0-9";
				monthCombobox.textInput.maxChars=2;
			});
			monthCombobox.addEventListener(IndexChangeEvent.CHANGE,function():void{
				//monthChange
				//setNowTime(true);
			});
			borderBox.addElement(monthCombobox);
			borderBox.addElement(MethodTimeChooser.createLabel("月"));
			
			this.addElement(borderBox);
			dateGroup=new DateGroup();
			dateGroup.addEventListener(Event.CHANGE,function():void{
				//dateChange
				//setNowTime();
			});
			this.addElement(dateGroup);
			
			borderBox=MethodTimeChooser.createBorderBox();
			hourNS=createNS(23);
			hourNS.addEventListener(Event.CHANGE,function():void{
				//hourChange
				//setNowTime();
			});
			borderBox.addElement(hourNS);
			borderBox.addElement(MethodTimeChooser.createLabel("时"));
			
			minuteNS=createNS(59);
			minuteNS.addEventListener(Event.CHANGE,function():void{
				//minuteChange
				//setNowTime();
			});
			borderBox.addElement(minuteNS);
			borderBox.addElement(MethodTimeChooser.createLabel("分"));
			
			secondNS=createNS(59);
			secondNS.addEventListener(Event.CHANGE,function():void{
				//secondChange
				//setNowTime();
			});
			
			borderBox.addElement(secondNS);
			borderBox.addElement(MethodTimeChooser.createLabel("秒"));
			this.addElement(borderBox);		
			
			//按钮 
			borderBox = MethodTimeChooser.createBorderBox();
			var clearBtn:Button = new Button();
			clearBtn.label = "清空";
			clearBtn.addEventListener(MouseEvent.CLICK, function():void {
				clearDate();
				closeWindow();
			});
			borderBox.addElement(clearBtn);
			var confirmBtn:Button = new Button();
			confirmBtn.label = "确定";
			confirmBtn.addEventListener(MouseEvent.CLICK, function():void {
				setNowTime();
				closeWindow();
			});
			var space:Spacer = new Spacer();
			space.percentWidth = 100;
			space.percentHeight = 100;
			borderBox.addElement(space);
			var cancelBtn:Button = new Button();
			cancelBtn.label = "取消";
			cancelBtn.addEventListener(MouseEvent.CLICK, function():void {
				closeWindow();
			});
			borderBox.addElement(confirmBtn);
			borderBox.addElement(cancelBtn);
			this.addElement(borderBox);
		}
		override protected function commitProperties():void{
			super.commitProperties();
			if(!selectedDate){
				if(selectedDateStr){
					_selectedDate=new Date(selectedDateStr);
				}else{
					_selectedDate=new Date();
				}
			}			
			this.yearCombobox.selectedItem=selectedDate.getFullYear();
			this.monthCombobox.selectedItem=selectedDate.getMonth()+1;
			this.dateGroup.selectedItem=selectedDate.getDate();
			this.dateGroup.nowTime=selectedDate;
			this.hourNS.value=selectedDate.getHours();
			this.minuteNS.value=selectedDate.getMinutes();
			this.secondNS.value=selectedDate.getSeconds();			
		}
		
		private function closeWindow():void {
			if(this.timeField)
				this.timeField.popTimeChooser(false);
		}
		
		private function clearDate():void {
			this.selectedDateStr = "";
			this.selectedDate = null;
		}
		
		
		private function setNowTime(month:Boolean=false):void{
			var YYYY:Number=this.yearCombobox.selectedItem;
			var MM:uint=this.monthCombobox.selectedItem;
			var DD:uint=this.dateGroup.selectedItem;
			var JJ:Number=this.hourNS.value;
			var NN:Number=this.minuteNS.value;
			var SS:Number=this.secondNS.value;
			this.selectedDate=new Date(YYYY,MM-1,DD,JJ,NN,SS);
			this.selectedDateStr=DateHelper.dateToString(selectedDate,formatString);
			
			if(month){
				dateGroup.nowTime=selectedDate;
			}
		}
		private function createNS(max:Number):NumericStepper{
			var ns:NumericStepper=new NumericStepper();
			ns.maximum=max;
			ns.width=45;
			return ns;			
		}
		override public function get baselinePosition():Number{
			return 120;
		}
	}
}